import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lcapp/model/event/event_bus.dart';
import 'package:lcapp/page/drawer_page.dart';
import 'package:lcapp/page/home/home_zixun_page.dart';
import 'package:lcapp/page/xianhuo/xianhuo.dart';
import 'package:lcapp/widget/asr_app_bar.dart';
import 'package:lcapp/widget/custom_indicator.dart';
import 'package:lcapp/widget/custom_tabs.dart';
import 'package:lcapp/widget/lc_gesture_detector.dart';

class HomeNewPage extends StatefulWidget {
  const HomeNewPage({Key? key}) : super(key: key);

  @override
  _HomeNewPageState createState() => _HomeNewPageState();
}

class _HomeNewPageState extends State<HomeNewPage> with SingleTickerProviderStateMixin,AutomaticKeepAliveClientMixin{
  TabController? tabController;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    tabController = TabController(length: 2, vsync: this);
  }
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Stack(
        children: [
          Positioned(child: Container(
              width: 1.sw,
              child: Container(
                decoration: const BoxDecoration(
                    image: DecorationImage(
                        image: AssetImage('assets/home/zx_top_bg.png'),
                        fit: BoxFit.fill
                    )
                ),
              )

          ),left: 0,right: 0,top:0,height: 400.h,),
          Positioned(child: Container(
              width: 1.sw,
              height: 1.sh,
              child: Container(
                // decoration: BoxDecoration(
                //     image: DecorationImage(
                //         image: AssetImage('assets/home/zx_top_bg.png'),
                //         fit: BoxFit.fill
                //     )
                // ),
                child: Column(
                  children: [
                    AsrAppBar(
                      backgroundColor: Colors.transparent,
                      barColor: Colors.white,
                      isLead: false,
                    ),
                    Expanded(
                        child: TabBarView(
                          controller: tabController,
                          children: const [
                            HomeZixunPage(),
                            XianHuoPage()
                          ],
                        )
                    )
                  ],
                ),
              )

          ),left: 0,right: 0,top:0,bottom: 0,),
          Positioned(
            child: AsrAppBar(
              backgroundColor: Colors.transparent,
              barColor: Colors.white,
              isLead: true,
              leadingWidth: 130.w,
              leading: lcGestureDetector(
                isLogin:true,
                onTap: (){
                  if(sliderDrawerKey!=null){
                    if(sliderDrawerKey.currentState!.isDrawerOpen){
                      sliderDrawerKey.currentState!.closeSlider();
                      eventBus.fire(HomeSliderChangeIndex(isOpen:false));
                    }else{
                      sliderDrawerKey.currentState!.openSlider();
                      eventBus.fire(HomeSliderChangeIndex(isOpen:true));
                    }
                  }
                },
                child: Container(
                  color: Colors.transparent,
                  padding: EdgeInsets.only(left: 34.w,),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Image.asset("assets/home/home_menu.png",fit: BoxFit.fitWidth,width: 50.w,)
                    ],
                  ),
                ),
              ),
              actions: [
                lcGestureDetector(
                  isLogin:true,
                  onTap: (){
                    pushPageName("SearchPage",arguments: tabController?.index==1?1:0);
                  },
                  child: Container(
                    width: 100.w,
                    color: Colors.transparent,
                    alignment: Alignment.centerRight,
                    padding: EdgeInsets.only(right: 30.w),
                    child: Image.asset("assets/home/home_search.png",width: 50.w,fit: BoxFit.fitWidth,),
                  ),
                )
              ],
              title: Container(
                // color: Colors.red,
                width: 380.w,
                child: TabBar(
                  controller: tabController!,
                  // indicator: CustomIndicator(
                  //     type: CustomIndicatorType.LineRect,
                  //     height: 2,
                  //     width: 30.w,
                  //     color: Colors.white),
                  labelColor: Colors.white,
                  labelStyle: TextStyle(
                    fontSize: 40.sp,
                    color: Colors.white,
                    fontWeight: FontWeight.w600,
                  ),
                  unselectedLabelColor: Colors.white,
                  unselectedLabelStyle: TextStyle(
                    fontSize: 30.sp,
                    color: Colors.white,
                    fontWeight: FontWeight.w400,
                  ),
                  tabs: const [
                    Text("资讯"),
                    Text("现货"),
                  ],
                ),
              ),
            ),
            left: 0,right: 0,top: 0,
          ),
        ],
      ),
    );
  }

  pushPageName(String name, {dynamic arguments}){
    Navigator.of(context).pushNamed(name,arguments: arguments);
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
